<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom String Types</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../supported_string_types.html" title="Supported String Types">
<link rel="prev" href="../supported_string_types.html" title="Supported String Types">
<link rel="next" href="../the___default_constructible__type_requirement.html" title="The Default Constructible Type Requirement">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../supported_string_types.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../supported_string_types.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../the___default_constructible__type_requirement.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="boost_convert.converters_detail.stream_converter.supported_string_types.custom_string_types"></a><a class="link" href="custom_string_types.html" title="Custom String Types">Custom
          String Types</a>
</h5></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">stream</span></code> accepts custom string and string-like
            types as input or output as long as they satisfy certain requirements.
          </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span> <span class="identifier">cnv</span><span class="special">;</span>
<span class="identifier">my_string</span>        <span class="identifier">my_str</span><span class="special">(</span><span class="string">"123"</span><span class="special">);</span>

<span class="identifier">cnv</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="number">2</span><span class="special">))(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">fixed</span><span class="special">);</span>

<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">my_str</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">my_string</span><span class="special">&gt;(</span> <span class="number">99.999</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">)</span> <span class="special">==</span> <span class="string">"100.00"</span><span class="special">);</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">my_string</span><span class="special">&gt;(</span> <span class="number">99.949</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">)</span> <span class="special">==</span>  <span class="string">"99.95"</span><span class="special">);</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">my_string</span><span class="special">&gt;(-</span><span class="number">99.949</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">)</span> <span class="special">==</span> <span class="string">"-99.95"</span><span class="special">);</span>
</pre>
<p>
          </p>
<p>
            When a string-like type is the source, then it needs to be a <span class="emphasis"><em>contiguous
            sequence</em></span> of the corresponding character type (<code class="computeroutput"><span class="keyword">char</span></code> or <code class="computeroutput"><span class="keyword">wchar_t</span></code>)
            accessible via <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>.
          </p>
<p>
            <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code> is implemented entirely
            in terms of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_streambuf</span></code> which, in turn, operates
            on a <span class="emphasis"><em>contiguous character sequence</em></span>, also called
            the <span class="emphasis"><em>buffer</em></span> (see <a href="http://en.cppreference.com/w/cpp/io/basic_streambuf" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_streambuf</span></code></a> for details).
            For efficiency reasons <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">stream</span></code>
            uses the provided (read-only) input string as the <span class="emphasis"><em>buffer</em></span>
            and, consequently, requires that provided input string to be a contiguous
            character sequence.
          </p>
<p>
            When a string is the target, then the described <span class="emphasis"><em>contiguous
            character sequence</em></span> requirement does not apply. Any type that
            provides
          </p>
<pre class="programlisting"><span class="identifier">MyType</span><span class="special">::</span><span class="identifier">MyType</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">beg</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">end</span><span class="special">)</span>
</pre>
<p>
            constructor can be deployed in type-to-string conversions.
          </p>
<p>
            See <a class="link" href="../../../performance/the_bigger_picture.html" title="The Bigger Picture">The
            Bigger Picture</a> chapter for the discussion of potential advantages
            of deploying custom strings.
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2020 Vladimir Batov<p>
        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../supported_string_types.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../supported_string_types.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../the___default_constructible__type_requirement.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
